package com.shiguiwu.springmybatis.spring.property.refresh;

import com.shiguiwu.springmybatis.spring.property.MailConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.sql.SQLException;
import java.util.stream.Stream;

/**
 * @description: 修改配置，动态生效
 * @author: stone
 * @date: Created by 2021/4/6 19:56
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.spring.property
 */
public class RefreshDbScopeTests {

    public static void main(String[] args) throws SQLException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        //讲自定义作用域注册到容器中
        context.getBeanFactory().registerScope(BeanRefreshScope.SCOPE_NAME, BeanRefreshScope.getInstance());

        context.register(MailConfig.class);
        //刷新mail的配置到Environment
        RefreshConfigUtil.refreshMailPropertySource(context);

        context.refresh();

        MailConfig bean = context.getBean(MailConfig.class);
        System.out.println("MailConfig的class对象：" + bean.getClass());

        System.out.println("多次调用username的效果-----------------------");

        Stream.of(1,2,3).forEach(e -> {
            System.err.println(bean + "-----------------------");

        });

        System.out.println("多次调用username的效果配置更新-----------------------");


        Stream.of(1,2,3).forEach(e -> {
            RefreshConfigUtil.updateDbConfig(context);
            System.err.println(bean + "-----------------------");

        });
    }
}
